﻿
using Xacml.Core;
using System;
using System.Xml;

namespace Xacml.Core.Policy
{
  public class ObligationElementReadWrite : XacmlElement
  {
    private AttributeAssignmentCollectionReadWrite _attributeAssignment = new AttributeAssignmentCollectionReadWrite();
    private string _obligationId;
    private Effect _fulfillOn;

    public virtual AttributeAssignmentCollectionReadWrite AttributeAssignment
    {
      get
      {
        return this._attributeAssignment;
      }
      set
      {
        this._attributeAssignment = value;
      }
    }

    public virtual Effect FulfillOn
    {
      get
      {
        return this._fulfillOn;
      }
      set
      {
        this._fulfillOn = value;
      }
    }

    public virtual string ObligationId
    {
      get
      {
        return this._obligationId;
      }
      set
      {
        this._obligationId = value;
      }
    }

    public override bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    public ObligationElementReadWrite(string obligationId, Effect fulfillOn, AttributeAssignmentCollectionReadWrite attributeAssignment)
    {
      this._obligationId = obligationId;
      this._fulfillOn = fulfillOn;
      this._attributeAssignment = attributeAssignment;
    }

    public ObligationElementReadWrite(XmlReader reader, XacmlVersion schemaVersion)
      : base(XacmlSchema.Policy, schemaVersion)
    {
      if (reader != null)
      {
        if (!(reader.LocalName == "Obligation") || !this.ValidateSchema(reader, schemaVersion))
          return;
        this._obligationId = reader.GetAttribute("ObligationId");
        this._fulfillOn = (Effect) Enum.Parse(typeof (Effect), reader.GetAttribute("FulfillOn"), false);
        while (reader.Read())
        {
          switch (reader.LocalName)
          {
            case "AttributeAssignment":
              this._attributeAssignment.Add(new AttributeAssignmentElementReadWrite(reader, schemaVersion));
              break;
          }
          if (reader.LocalName == "Obligation" && reader.NodeType == XmlNodeType.EndElement)
            break;
        }
      }
      else
        this._obligationId = "[TODO]: Add Obligation ID";
    }
  }
}
